// pages/home/pages/party10Detail/party10Detail.js
var r = require('../../../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    list: [],
    checkedList: []
  },

  // 选择
  choose(e) {
    // console.log(e.target.dataset.num1)
    // console.log(e.target.dataset.num2)
    // console.log(e.target.dataset.type)
    let index1 = e.target.dataset.num1
    let index2 = e.target.dataset.num2
    let type = e.target.dataset.type

    let list = this.data.list
    let checkedList = this.data.checkedList

    // 先判断题型
    if (type == 1) {//单选
      list[index1].option.forEach(item => {
        item.check = false
      });
      list[index1].option[index2].check = true //修改当前题目状态
      checkedList[index1].choose = list[index1].option[index2].answer //当前题目单选选中赋值
    } else if (type == 2) { //多选
      list[index1].option[index2].check = !list[index1].option[index2].check //修改当前题目状态
      let datas = []
      list[index1].option.forEach(item2 => { //当前题目多选选中
        if (item2.check == true) {
          datas.push(item2.answer)
        }
      });
      checkedList[index1].choose = datas.toString() //当前题目多选选中赋值
    }else if(type == 3){//单选
      console.log(e.detail.value)
      list[index1].val = e.detail.value
      checkedList[index1].choose = e.detail.value //当前题目填空题赋值
    }
    console.log(checkedList)
    // console.log(list)

    this.setData({
      list: list,
      checkedList: checkedList
    })
  },

  // 提交
  submit() {
    var that = this
    // console.log(this.data.checkedList)
    for (let i = 0; i < this.data.checkedList.length; i++) {
      // console.log(this.data.checkedList[i].choose)
      if (!this.data.checkedList[i].choose || this.data.checkedList[i].choose == '') {
        wx.showToast({
          title: '还有问题未投票，请填完后提交！',
          icon:'none'
        })
        return
      }
    }
    that.handOver()
  },

  // 交卷
  handOver() {
    // console.log(this.data.checkedList)
    // console.log(this.data.paperId)
    var that = this
    // 获取试题
    r.toPost('/surveyPaper/commit', {
      userId:wx.getStorageSync('userId'),
      paperId: that.data.id,
      examAnswer: JSON.stringify(that.data.checkedList)
    }, (res) => {
      wx.showToast({
        title: '提交成功',
        icon: 'none'
      })
      setTimeout(function(){
        wx.navigateBack({
          delta: 1,
        })
      },1000)
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取试题
    r.toGet('/surveyPaper/subject', {
      paperId: options.id
    }, (res) => {
      // console.log(res)
      let list = res.retData.subjectVo

      // let list = 
      // [
      //   {id: "1433348740915359746",name: "党员在留党察看期间没有（）。",option:[
      //     {answer: "A", check: false, title: "表决权"},{answer: "B", check: false, title: "选举权"},{answer: "C", check: false, title: "被选举权"}
      //   ],type: "2"},

      //   {id: "43334646678889677",name: "中国共产党第一次全国代表大会于（）在上海举行。",option:[
      //     {answer: "A", check: false, title: "1921年7月1日"},{answer: "B", check: false, title: "1921年7月23日"},{answer: "C", check: false, title: "1921年7月31日"}
      //   ],type: "1"},

      //   {id: "1433348411465363457",name: "被称为中国共产党历史上的两个伟大转折点是（）。",option:[
      //     {answer: "A", check: false, title: "八七会议"},{answer: "B", check: false, title: "遵义会议"},{answer: "C", check: false, title: "十一届三中全会"}
      //   ],type: "1"},

      //   {id: "54545345345343545",name: "开发者的姓名",type: "3",val:''},
      //   {id: "54545345345343545",name: "开发者的年龄",type: "3",val:''},
      // ]
      let checkedList = []
      list.forEach(item => {
        let str = {
          id: item.id,
          choose: ''
        }
        checkedList.push(str)
      });
      // console.log(checkedList)
      this.setData({
        id:options.id,
        list: list,
        checkedList: checkedList
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})